program gmm_prod
	version 11

	syntax varlist [if], at(name) rhs(varlist)
	
	local m1: word 1 of `varlist'
	local m2: word 2 of `varlist'
	local m3: word 3 of `varlist'
	local m4: word 4 of `varlist'

	local qg: word 1 of `rhs'
	local l: word 2 of `rhs'
	local k: word 3 of `rhs'
	local i: word 4 of `rhs'
	local qg_1: word 5 of `rhs'
	local l_1: word 6 of `rhs'
	local k_1: word 7 of `rhs'
	local i_1: word 8 of `rhs'
	local cpc3: word 9 of `rhs'
	
	local p_1: word 10 of `rhs'
	local i_2: word 11 of `rhs'

	tempname al ak ai
	scalar `al'=`at'[1,1] 
	scalar `ak'=`at'[1,2]
	scalar `ai'=`at'[1,3]

	tempvar w w_1 csi  

	quietly gen double `w'=`qg'-`al'*`l'-`ak'*`k'-`ai'*`i' `if'
	quietly gen double `w_1'=`qg_1'-`al'*`l_1'-`ak'*`k_1'-`ai'*`i_1'  `if'
	quietly reg `w' `w_1' i.`cpc3' `if'

	quietly predict double `csi' `if', resid

	quietly replace `m1'=`l'*`csi' `if'	
	quietly replace `m2'=`k'*`csi' `if'
	quietly replace `m3'=`p_1'*`csi' `if'	
	quietly replace `m4'=`i_2'*`csi' `if'	


	
end

